setwd("../")
rm(list = ls())

# # only need to install once
# devtools::install_github("cloudyr/pyMTurkR", dependencies=TRUE)
# devtools::install_github("Luwei-Ying/validateIt", dependencies=TRUE)
# load R package
library(validateIt)

### load topic model
load('models/stm10k1it.Rdata')

### prepare tasks
newMass <- combMass(stm10k1it)
R4WSIstm10k1it <- validateTopic(type = "R4WSI", n = 500, vocab = newMass[[1]], beta = newMass[[2]], thres = 20)
goldR4WSI <- read.csv("goldstandard/goldR4WSI.csv", stringsAsFactors = FALSE)
R4WSIstm10k1it <- mixGold(tasks = R4WSIstm10k1it, golds = goldR4WSI)
R4WSIstm10k1it <- recordTasks(type = "R4WSI", tasks = R4WSIstm10k1it,
                           path = "topicrecords/R4WSIstm10k1it.Rdata")

# ##### interact with Mturk from here #####
# ### environment
# library(pyMTurkR)
# Sys.setenv(AWS_ACCESS_KEY_ID = "AWS_ACCESS_KEY_ID")
# Sys.setenv(AWS_SECRET_ACCESS_KEY = "AWS_SECRET_ACCESS_KEY")
# options(pyMTurkR.sandbox = T) # Change sandbox = F when ready to run on MTurk
# AccountBalance()
# 
# ### send tasks (first trial)
# R4WSIstm10k1itids <- sendTasks(hit_type = 'find_from_dashboard', hit_layout = 'find_from_dashboard', type = "R4WSI",
#                             tasksrecord = R4WSIstm10k1it, HITidspath = "HITids/R4WSIstm10k1itids1.Rdata")
# ### extend HITs if needed
# for(i in R4WSIstm10k1itids[[1]]){
#   ExtendHIT(hit = i, add.seconds = 60*60*24)
# }
# 
# load("HITids/R4WSIstm10k1itids1.Rdata")
# R4WSIstm10k1itresults1 <- getResults(batch_id = "R4WSIstm10k1it1", hit_ids = HITids, retry = FALSE)
# write.csv(R4WSIstm10k1itresults1, "../../1_replication/1_topicvalidation/topicresults/R4WSIstm10k1itresults1.csv", row.names = FALSE)
# evalResults(results = R4WSIstm10k1itresults1, key = R4WSIstm10k1it, type = "R4WSI")
# 
# ### send tasks (second trial)
# R4WSIstm10k1itids <- sendTasks(hit_type = 'find_from_dashboard', hit_layout = 'find_from_dashboard', type = "R4WSI",
#                             tasksrecord = R4WSIstm10k1it, HITidspath = "HITids/R4WSIstm10k1itids2.Rdata")
# ### extend HITs if needed
# for(i in R4WSIstm10k1itids[[1]]){
#   ExtendHIT(hit = i, add.seconds = 60*60*24)
# }
# 
# load("HITids/R4WSIstm10k1itids2.Rdata")
# R4WSIstm10k1itresults2 <- getResults(batch_id = "R4WSIstm10k1it2", hit_ids = HITids, retry = FALSE)
# write.csv(R4WSIstm10k1itresults2, "../../1_replication/1_topicvalidation/topicresults/R4WSIstm10k1itresults2.csv", row.names = FALSE)
# evalResults(results = R4WSIstm10k1itresults2, key = R4WSIstm10k1it, type = "R4WSI")